/*
 * Author: Honrun
 */
#include "stm32f4xx_hal.h"
#include "DevicesDelay.h"
#include "DevicesPower.h"


static PowerInfoType st_typePowerInfo = {0};


void vPowerInit(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    /* GPIO Ports Clock Enable */
    __HAL_RCC_GPIOD_CLK_ENABLE();

    /*Configure GPIO pin Output Level */
    HAL_GPIO_WritePin(POWER_COM_GPIO_Port,   POWER_COM_GPIO_Pin,   GPIO_PIN_RESET);
    HAL_GPIO_WritePin(POWER_VCC1_GPIO_Port,  POWER_VCC1_GPIO_Pin,  GPIO_PIN_RESET);
    HAL_GPIO_WritePin(POWER_VCC2H_GPIO_Port, POWER_VCC2H_GPIO_Pin, GPIO_PIN_RESET);
    HAL_GPIO_WritePin(POWER_VCC2L_GPIO_Port, POWER_VCC2L_GPIO_Pin, GPIO_PIN_RESET);
    HAL_GPIO_WritePin(POWER_VCCQ_GPIO_Port,  POWER_VCCQ_GPIO_Pin,  GPIO_PIN_RESET);

    /*Configure GPIO pin : PtPin */
    GPIO_InitStruct.Pin = POWER_COM_GPIO_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(POWER_COM_GPIO_Port, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = POWER_VCC1_GPIO_Pin;
    HAL_GPIO_Init(POWER_VCC1_GPIO_Port, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = POWER_VCC2H_GPIO_Pin;
    HAL_GPIO_Init(POWER_VCC2H_GPIO_Port, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = POWER_VCC2L_GPIO_Pin;
    HAL_GPIO_Init(POWER_VCC2L_GPIO_Port, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = POWER_VCCQ_GPIO_Pin;
    HAL_GPIO_Init(POWER_VCCQ_GPIO_Port, &GPIO_InitStruct);


    /* 关闭所有通道 */
    vPowerClose(POWER_CHANNEL_ALL);
}

void vPowerOpen(uint32_t uiChannel)
{
    if(uiChannel & POWER_CHANNEL_COM)
    {
        POWER_COM_ENABLE();
        st_typePowerInfo.com = ENABLE;
    }

    if(uiChannel & POWER_CHANNEL_VCC1)
    {
        POWER_VCC1_ENABLE();
        vDelayMs(4);
        st_typePowerInfo.vdd1 = ENABLE;
    }

    if(uiChannel & POWER_CHANNEL_VCC2H)
    {
        POWER_VCC2H_ENABLE();
        vDelayMs(4);
        st_typePowerInfo.vdd2H = ENABLE;
    }

    if(uiChannel & POWER_CHANNEL_VCC2L)
    {
        POWER_VCC2L_ENABLE();
        vDelayMs(4);
        st_typePowerInfo.vdd2L = ENABLE;
    }

    if(uiChannel & POWER_CHANNEL_VCCQ)
    {
        POWER_VCCQ_ENABLE();
        vDelayMs(4);
        st_typePowerInfo.vddQ = ENABLE;
    }
}

void vPowerClose(uint32_t uiChannel)
{
    if(uiChannel & POWER_CHANNEL_COM)
    {
        POWER_COM_DISABLE();
        st_typePowerInfo.com = DISABLE;
    }

    if(uiChannel & POWER_CHANNEL_VCC1)
    {
        POWER_VCC1_DISABLE();
        vDelayMs(4);
        st_typePowerInfo.vdd1 = DISABLE;
    }

    if(uiChannel & POWER_CHANNEL_VCC2H)
    {
        POWER_VCC2H_DISABLE();
        vDelayMs(4);
        st_typePowerInfo.vdd2H = DISABLE;
    }

    if(uiChannel & POWER_CHANNEL_VCC2L)
    {
        POWER_VCC2L_DISABLE();
        vDelayMs(4);
        st_typePowerInfo.vdd2L = DISABLE;
    }

    if(uiChannel & POWER_CHANNEL_VCCQ)
    {
        POWER_VCCQ_DISABLE();
        vDelayMs(4);
        st_typePowerInfo.vddQ = DISABLE;
    }
}

PowerInfoType *ptypePowerInfoGet(void)
{
    return &st_typePowerInfo;
}
